package edu.fzu.costlog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.fzu.costlog.domain.CostCategory;
import edu.fzu.costlog.domain.CostLog;
import edu.fzu.costlog.service.CostCategoryService;
import edu.fzu.costlog.service.CostCategoryServiceImpl;
import edu.fzu.costlog.service.CostLogService;
import edu.fzu.costlog.service.CostLogServiceImpl;

public class CostLogMgrServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String act = request.getParameter("act");

		if (act.equals("save")) {

			CostLog log = new CostLog();

			log.setLogDesc(request.getParameter("logdesc"));
			log.setTypeId(Integer.parseInt(request.getParameter("costtype")));
			log.setCostAmount(Double.parseDouble(request
					.getParameter("logamount")));
			log.setCostTime(new Date());

			CostLogService logService = new CostLogServiceImpl();
			logService.saveCostLog(log);

			response.sendRedirect("costLogMgr?act=loadall");
		} else if (act.equals("loadall")) {

			CostLogService logService = new CostLogServiceImpl();
			List<CostLog> logList = logService.loadall();

			request.setAttribute("logs", logList);

			request.getRequestDispatcher("show_costlogs.jsp").forward(request,
					response);
		} else if (act.equals("input_category")) {
			CostCategoryService categoryService = new CostCategoryServiceImpl();
			List<CostCategory> categoryList = categoryService.loadall();

			request.setAttribute("categorys", categoryList);
			request.getRequestDispatcher("input_costlog.jsp").forward(request,
					response);
		} else if (act.equals("del")) {

			int logId = Integer.parseInt(request.getParameter("logid"));

			CostLogService logService = new CostLogServiceImpl();
			logService.removeCostLog(logId);

			response.sendRedirect("costLogMgr?act=loadall");
		} else if (act.equals("getLogById")) {

			int logId = Integer.parseInt(request.getParameter("logid"));

			CostLogService logService = new CostLogServiceImpl();
			request.setAttribute("log", logService.getCostLogById(logId));

			request.getRequestDispatcher("update_costlog.jsp").forward(request,
					response);
		} else if (act.equals("update")) {

			CostLog log = new CostLog();

			log.setLogId(Integer.parseInt(request.getParameter("logid")));
			log.setLogDesc(request.getParameter("logdesc"));
			log.setTypeId(Integer.parseInt(request.getParameter("costtype")));
			log.setCostAmount(Double.parseDouble(request
					.getParameter("logamount")));
			log.setCostTime(new Date());

			CostLogService logService = new CostLogServiceImpl();
			logService.updateCostLog(log);

			response.sendRedirect("costLogMgr?act=loadall");
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
